package com.lihuan.common.util;


import cn.hutool.json.JSONUtil;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * 
 * @since 2020年5月14日 下午10:25:23
 * @version v1.0.0
 * @author lihuan
 */
@Setter
@Getter
@SuppressWarnings("serial")
public class Result implements Serializable {
    
    public static final Result SUCCESS = new Result(Messages.SUCCESS);
    
    /** 操作码 */

    private int code;
    
    /** 结果消息 */

    private String message;

    
    public Result() {
    }

    public Result(String message) {
        this(Code.SUCCESS, message);
    }
    
    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }
    
    public Result(Throwable e) {
        this.code = Code.ERROR_500;
        this.message = e.getMessage();
    }
    
    public String toString() {
        // StringBuilder sb = new StringBuilder();
        // sb.append("{\"code\":").append(code).append(",\"message\":\"").append(message).append("\"}");
        // return sb.toString();
        return JSONUtil.toJsonStr(this);
//        return JSONUtil.toJsonStr(this);
    }
    
    public enum Status {
        OK(200, "操作成功！"), 
        FAILED(500, "操作失败！"), 
        VALIDATE_FAILED(404, "参数检验失败"),
        UNAUTHORIZED(401, "暂未登录或token已经过期"),
        FORBIDDEN(403, "没有相关权限"),
    	
        TIMEOUT(408, "服务器连接超时！"), 
        LOGIN(1000, "登录成功！"), 
        PERMISSION(1001, "对不起您没有权限,请联系管理员！"), 
        VALIDATE(1002, "校验失败！"), 
        DATABASE(1003, "数据库服务异常！"), 
        DATABASE_CONNECTION_TIMEDOUT(1004, "数据库连接异常！"), 
        DATABASE_CONNECT(1004, "数据库连接异常！"),
        LOGIN_FAIL(1005, "登录失败！");
        
        /** 操作码 */
        private @Setter @Getter int code;
        
        /** 结果消息 */
        private @Setter @Getter String message;
        
        private Status(int code, String message) {
            this.code = code;
            this.message = message;
        }
        
    }
}